package com.orifine.sql.simple;

import java.net.URLEncoder;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import com.bowos.utils.SpringUtils;

/**
 * 参数
 * @author http:www.bowos.com
 *
 */
public class SimpleParam {
	private Map<String, Object> params;
	
	public SimpleParam(){
		params = new HashMap<String, Object>();
	}
	
	public SimpleParam(String key, Object value){
		params = new HashMap<String, Object>();
		addParam(key, value);
	}
	
	public SimpleParam(Map<String, Object> param){
		params = new HashMap<String, Object>();
	}
	
	public SimpleParam(SimpleParam bowosParam){
		params = new HashMap<String, Object>();
		addParam(bowosParam);
	}
	
	public SimpleParam setParam(String key, Object value){
		if(value!=null){
			params.put(key, value);
		}
		return this;
	}
	
	public SimpleParam setParam(Map<String, Object> param){
		Iterator<String> itor = param.keySet().iterator();
		while(itor.hasNext()){
			String key = itor.next();
			setParam(key,param.get(key));
		}
		return this;
	}
	
	public SimpleParam setParam(SimpleParam bowosParam){
		Iterator<String> itor = bowosParam.params.keySet().iterator();
		while(itor.hasNext()){
			String key = itor.next();
			setParam(key,bowosParam.params.get(key));
		}
		return this;
	}
	
	public SimpleParam addParam(String key, Object value){
		Object o = params.get(key);
		if (o != null &&  o instanceof Set) {
			Set<Object> set = (Set<Object>)o;
			set.add(value);
		} else {
			Set<Object> set = new HashSet<Object>();
			set.add(value);
			params.put(key, set);
		}
		return this;
	}
	
	public SimpleParam addParam(Map<String, Object> param){
		Iterator<String> itor = param.keySet().iterator();
		while(itor.hasNext()){
			String key = itor.next();
			addParam(key,param.get(key));
		}
		return this;
	}
	
	public SimpleParam addParam(SimpleParam bowosParam){
		Iterator<String> itor = bowosParam.params.keySet().iterator();
		while(itor.hasNext()){
			String key = itor.next();
			addParam(key,bowosParam.params.get(key));
		}
		return this;
	}
	
	public Object getParam(String key){
		return params.get(key);
	}
	
	public SimpleParam removeParam(String key){
		params.remove(key);
		return this;
	}
	
	public Boolean exist(){
		if(!params.isEmpty()){
			return true;
		} else {
			return false;
		}
	}
	
	public String getPrefix(){
		if(!params.isEmpty()){
			return "?";
		} else {
			return "";
		}
	}
	
	public String toString(){
		return toString(null);
	}
	
	public String toPrefixString(){
		return getPrefix() + toString();
	}
	
	public String toString(String prefix){
		StringBuffer buffer = new StringBuffer();
//		if(prefix && params.size() > 0){
//			buffer.append("?");
//		}
		Iterator<String> iterator = params.keySet().iterator();
		boolean isFirst = true;
		while(iterator.hasNext()){
			String key = iterator.next();
			Object value = params.get(key);
			if(value instanceof Set){
				Iterator<String> iterator2 = ((Set<String>)value).iterator();
				while(iterator2.hasNext()){
					if(!isFirst) {
						buffer.append("&");
					} else {
						isFirst = false;
					}
					if(prefix != null){
						buffer.append(prefix+"_");
					}
					buffer.append(key+"="+iterator2.next());
				}
			} else {
				if(!isFirst) {
					buffer.append("&");
				} else {
					isFirst = false;
				}
				if(prefix != null){
					buffer.append(prefix+"_");
				}
				buffer.append(key+"="+value);
			}
		}
		return buffer.toString();
	}
	
	public String getString(){
		return toString();
	}
	
	public static void main(String[] args) {
		SimpleParam param = new SimpleParam();
		param.setParam("id","1")
			.setParam("username","admin")
			.setParam("password","123456").setParam("keys","中国");
		System.out.println("setParam:"+param.toString());
		
		SimpleParam param2 = new SimpleParam();
		param2.addParam("id", "1")
			.addParam("id", "2")
			.addParam("id", "3");
		System.out.println("addParam:"+param2.toString("bowos"));
	}
}
